<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Progressive Play Server
 * Home Controller
 * /controllers/home.php
 * By: Yukwunhang
 */
class Home extends MY_Controller
{
	// Constructor
	function Home()
	{
		parent::MY_Controller();
		// Load models/libraries/helpers
		$this->load->model('player_model', 'player');
		$this->load->model('weapon_model', 'weapon');
		$this->load->model('stats_status_model', 'status');
		$this->load->library('soldat_refresh');
	}
	// Index - default method (to top players)
	function index()
	{
		// Default home page - top players
		$this->players();
	}
	
	/**
	 * Top players
	 * @return 
	 */
	function players()
	{
		// Set up
		$rows_shown = 40; // Number of players shown
		$this->data['rows_shown'] = $rows_shown;
		$offset = 0; // Start from where?
		$sort = 'rating'; // Sorting reference
		
		// Get top player stats
		$this->data['player'] = $this->player->get_top_player_stats($rows_shown, $offset, $sort);
		
		// Load view, display.
		$this->template->write('title', 'Progressive Play Server | Top Players');
		$this->template->write_view('content', 'player/top_players_table_view', $this->data);
		$this->template->render();
	}
	
	/**
	 * Weapons Usage
	 * @return 
	 */
	function weapons()
	{
		// Get weapon stats
		$this->data['weapon']= $this->weapon->get_weapon_stats();
		
		// Load view, display.
		$this->template->write('title', 'Progressive Play Server | Weapons Statistics');
		$this->template->write_view('content', 'weapon/weapon_stats_table_view', $this->data);
		$this->template->render();
	}
	
	/**
	 * Server Snapshot
	 * @return 
	 */
	function servers()
	{
		// Get live server info
		$this->data['server'] = $this->soldat_refresh->refresh();
		
		//  Extract IPs from the players array into a single array
		for ($i = 0; $i < $this->data['server']['players']; $i++)
		{
			$ip[$i] = $this->data['server']['player'][$i]['ip'];
		}
		
		// Get user_id and assoc. them back into the players array
		if (isset($ip))
		{
			$ip_id = $this->player->get_ids_by_ip_array($ip);
		}
		for ($i = 0; $i < $this->data['server']['players']; $i++)
		{
			for ($j = 0; $j < count($ip_id); $j++)
			{
				if ($this->data['server']['player'][$i]['ip'] === $ip_id[$j]['ip'])
				{
					$this->data['server']['player'][$i]['user_id'] = $ip_id[$j]['user_id'];
					break;
				}
			}
		}
		// Get Stats ON/OFF Status
		$this->data['stats_status'] = $this->status->get_stats_status();
		
		// Load view, display.
		$this->template->write('title', 'Progressive Play Server | Server Snapshot');
		$this->template->write_view('content', 'server_info_table', $this->data);
		$this->template->render();
	}
	
	function clans()
	{
		// Load view, display.
		$this->template->write('title', 'Progressive Play Server | Top Clans');
		$this->template->write('content', '<p class="warning">Working In Progress</p>');
		$this->template->render();
	}
}
/*
 * End of file
 */